<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" th:href="@{/assets/edu/layui/css/layui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/assets/edu/css/css.css}">
    <script type="text/javascript" th:src="@{/assets/edu/js/jquery-1.11.3.js}"></script>

    <script type="text/javascript" th:src="@{/assets/edu/js/md5.min.js}"></script>
    <!-- common.js -->
    <script type="text/javascript" th:src="@{/assets/edu/js/common.js}"></script>

    <script type="text/javascript" th:src="@{/assets/edu/layui/layui.js}"></script>
</head>
<body>
<div class="pad10">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-inline">
                    <input type="password" class="layui-input" id="old_password">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" class="layui-input" id="new_password">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" class="layui-input" id="new_password_repeat">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"></label>
                <a href="javascript:savePsd();" class="layui-btn ayui-btn-normal bg-blue ">
                    <i class="iconfont"></i> 保存密码</a>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use('layer', function () {
        var layer = layui.layer;
    });

    function savePsd() {
        var newPassword = $("#new_password").val();
        var newPasswordRepeat = $("#new_password_repeat").val();
        var oldPassword = $("#old_password").val();
        if (oldPassword == "") {
            layer.msg("请输入原密码！");
            return;
        }
        if (newPassword == "") {
            layer.msg("请输入新密码！");
            return;
        }
        if (newPasswordRepeat == "") {
            layer.msg("请输入确认密码！");
            return;
        }
        if (newPassword != newPasswordRepeat) {
            layer.msg("新密码和确认密码不一致！");
            return;
        }
        var salt = g_passsword_salt;
        var str1 = "" + salt.charAt(0) + salt.charAt(2) + newPassword + salt.charAt(5) + salt.charAt(4);
        var str2 = "" + salt.charAt(0) + salt.charAt(2) + oldPassword + salt.charAt(5) + salt.charAt(4);
        newPassword = md5(str1);
        oldPassword = md5(str2);
        layer.confirm('确定要更新密码吗!', {
            btn: ['确定', '取消']
        }, function (index, layero) {
            $.ajax({
                url: "/user/updatePassword",
                type: "post",
                dataType: "json",
                data: {
                    "oldPassword": oldPassword,
                    "newPassword": newPassword
                },
                success: function (data) {
                    if (data.code == 90000) {
                        layer.msg("修改成功！！");
                        $("#new_password").val("");
                        $("#new_password_repeat").val("");
                        $("#old_password").val("");
                    } else if (data.code == 30002) {
                        layer.msg(data.msg);
                        setTimeout(function () {
                            top.location.href = "/login";
                        }, 5000);
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (data) {
                    layer.msg("服务异常");
                }
            });
        });
    }
</script>
</body>
</html>